package com.aat.dizena.assist.web.controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 基础的controller父类
 * @info java spring aop
 * @update none
 */
public abstract class BaseController {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	public final String SMS_DEVID = getSession().getId() + "_devid";

	public final String SMS_USER = getSession().getId() + "_user";

	public final String WEB_LOGIN_INDEX = "/base/login";
	public final String WEB_MASTER_INDEX = "/master/index";

	public boolean isLoginStatus() {
		Object o = getSession().getAttribute(SMS_USER);
		return o == null ? false : true;
	}


	protected ServletContext getApplication(HttpServletRequest request) {
		return request.getServletContext();
	}

	public HttpSession getSession() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest().getSession();
	}
	
	public void addSessionObj(String key,Object obj) {
		String sid=getSession().getId();
		getSession().setAttribute(sid+key, obj);
	}
	
	public <T> T getSessionObj(String key, Class<T> cls) {
		String sid=getSession().getId();
		Object obj = getSession().getAttribute(sid+key);
		if(obj!=null){
			return cls.cast(obj);
		}
		return null;
	}

	public boolean isEmpty(String str) {
		if (isNotEmpty(str)) {
			return false;
		}
		return true;
	}

	public boolean isNotEmpty(String str) {
		if (str != null && str.length() > 0) {
			return true;
		}
		return false;
	}

	public char[] noSort(char[] obj) {
		char obj1[] = new char[obj.length];
		for (int i = obj.length - 1; i >= 0; i--) {
			int k = (int) (Math.random() * i);// 生成的k值
			obj1[i] = obj[k];// 取得数组前面的 10－i 个的其中一个值
			obj[k] = obj[i];// 将生成不到的数组值替换已经生成的数组值
			obj[i] = obj1[i];// 已经生成的放入再也生成不到的地方
		}
		return obj1;
	}

	public String getOneKeyS() {
		char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
		String str = "";
		chars = noSort(chars);// 无序
		for (int j = 0; j < chars.length; j++) {
			str += chars[j];
		}
		String timestr = System.currentTimeMillis() + "";
		String tmp = "";
		for (int i = 0; i < 4; i++) {
			tmp += str.substring(i * 3, i * 3 + 3) + timestr.substring(i * 3, i * 3 + 3);
		}
		tmp += timestr.substring(4 * 3, timestr.length()) + (int) (Math.random() * 1000);
		return tmp;
	}

	public static String noSortStr(int n) {
		char[] obj = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
		char obj1[] = new char[obj.length];
		for (int i = obj.length - 1; i >= 0; i--) {
			int k = (int) (Math.random() * i);// 生成的k值
			obj1[i] = obj[k];// 取得数组前面的 10－i 个的其中一个值
			obj[k] = obj[i];// 将生成不到的数组值替换已经生成的数组值
			obj[i] = obj1[i];// 已经生成的放入再也生成不到的地方
		}
		String tmp = new String(obj1);
		if (n > obj.length) {
			n = obj.length - 1;
		}
		return tmp.substring(0, n);
	}

}
